package com.flyee.utils.depot
{
	import com.flyee.utils.DepotLoader;
	import com.flyee.utils.MyLog;
	import com.flyee.viewer.MainMenuViewer;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.core.Application;
	import mx.core.BitmapAsset;
	import mx.core.UIComponent;
	
	public class SandTableDepot extends EventDispatcher
	{
		private static var _instance:SandTableDepot = null;
		private var depotLoader:DepotLoader = null;
		private var isComplete:Boolean = false;
		private var callFun:Function = null;
		public function SandTableDepot()
		{
			if(_instance){
				throw new Error("singleton error!!!");
			}
		}
		
		public static function getInstance():SandTableDepot{
			if(!_instance){
				_instance = new SandTableDepot();
			}
			return _instance;
		}
		
		public function load(url:String,root:UIComponent = null):void{
			depotLoader = new DepotLoader();
			depotLoader.loadByURL(url,root);
			depotLoader.addEventListener(Event.COMPLETE,completeEvent,false,0,true);
		}
		
		public function callFunction(callFun:Function):void{
			this.callFun = callFun;
			if(isComplete){
				this.callFun.call();
			}
		}
		
		public function getClassByName(name:String):Class{
			if(isComplete){
				var MCClass:Class = depotLoader.getClassByName(name);
				return MCClass;
			}
			return null;
		}
		
		public function getMovieClip(name:String):MovieClip{
			if(isComplete){
				var MCClass:Class = depotLoader.getClassByName(name);
				if(MCClass){
					var mc:MovieClip = new MCClass();
					return mc;
				}
			}
			MyLog.error("not found that you want mc in depot by"+name+"!!!!!!")
			return null;
		}
		
		public function getBitmapData(name:String):BitmapData{
			if(isComplete){
				var bitdata:Class = depotLoader.getClassByName(name);
				if(bitdata){
					var data:BitmapData = new bitdata(40,40);
					return data;
				}
			}
			MyLog.error("not found that you want bitmapdata in depot by"+name+"!!!!!!")
			return null;
		}
		
		public function getBitmaAsset(name:String):BitmapAsset{
			if(isComplete){
				var bitdata:Class = depotLoader.getClassByName(name);
				if(bitdata){
					var data:BitmapData = new bitdata(40,40);
					var asset:BitmapAsset = new BitmapAsset(data);
					return asset;
				}
			}
			MyLog.error("not found that you want bitmapdata in depot by"+name+"!!!!!!")
			return null;
		}
		
		public function getSimpleButton(name:String):SimpleButton{
			if(isComplete){
				var button:Class = depotLoader.getClassByName(name);
				if(button){
					var btn:SimpleButton = new button();
					return btn;
				}
			}
			MyLog.error("not found that you want simplebutton in depot by"+name+"!!!!!!")
			return null;
		}
		
		private function completeEvent(event:Event):void{
			isComplete = true;
			if(this.callFun != null){
				this.callFun.call();
			}
			this.dispatchEvent(event);
		}
	}
}